#!/usr/bin/env bash
###
 # @Author: Loganli
 # @Gitee: https://gitee.com/attacker
 # @email: admin@attacker.club
 # @Date: 2025-04-24 09:57:31
 # @LastEditTime: 2025-05-04 21:13:47
 # @Description: AWS Linux 2 环境下的 Zabbix 部署脚本
### 


CURRENT_DIR=$(
    cd "$(dirname "$0")"
    pwd
)

function logger() {
    case "$1" in
    "error" | "red")
        echo -e "\e[1;31m$2\e[0m"
        echo "$2" >>${CURRENT_DIR}/install.log
        exit 1
        ;;
    "warning" | "yellow")
        echo -e "\e[1;33m$2\e[0m"
        echo "$2" >>${CURRENT_DIR}/install.log
        ;;
    "success" | "green")
        echo -e "\e[1;32m$2\e[0m"
        echo "$2" >>${CURRENT_DIR}/install.log
        ;;
    "info" | "blue")
        echo -e "\e[1;34m$2\e[0m"
        echo "$2" >>${CURRENT_DIR}/install.log
        ;;
    esac
}

# 判断是否root用户
if [ $(id -u) -ne 0 ]; then
    logger error "########## This script must be run as root !!! ##########"
fi

# 环境检查
echo "time: $(date)"

if which getenforce && [ $(getenforce) == "Enforcing" ]; then
    logger info "... 关闭 SELINUX"
    setenforce 0
    sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
fi

>${CURRENT_DIR}/install.log
logger info "======================= 开始安装 =======================" 2>&1 | tee -a ${CURRENT_DIR}/install.log

logger info "检查 是否存在离线包 [docker_offline.tar.gz]"
if [ -f ${CURRENT_DIR}/docker_offline.tar.gz ]; then
    tar zxf docker_offline.tar.gz
    chmod +x docker-install.sh && ./docker-install.sh
    logger success "离线docker 安装成功"
    \cp docker-compose /usr/local/sbin/docker-compose
    chmod +x /usr/local/sbin/docker-compose
    logger success "离线docker-compose 安装成功"
else
    # 检查是否已经安装 Docker
    if [ -x "$(command -v docker)" ]; then
        logger info "Docker 已经安装"
    else
        # 检查系统是否为 Amazon Linux
        if grep -q "Amazon Linux" /etc/os-release; then
            logger info "检测到 Amazon Linux 系统，使用 yum 安装 Docker"
            # 更新 yum 包
            yum update -y
            # 安装 docker 及相关依赖
            yum install -y docker
            # 启动 docker 服务
            systemctl start docker
            systemctl enable docker
            # 添加当前用户到 docker 组
            usermod -aG docker $(whoami)
            logger success "Docker 安装成功"
        else
            # 下载 Docker 二进制安装包（非 Amazon Linux 系统）
            curl -fsSL https://get.docker.com | sh
            logger success "安装 Docker"
        fi
    fi

    # 检查是否已经安装 Docker Compose
    if [ -x "$(command -v docker-compose)" ]; then
        logger info "Docker Compose 已经安装"
    else
        # 检查系统是否为 Amazon Linux
        if grep -q "Amazon Linux" /etc/os-release; then
            logger info "使用 yum 安装 Docker Compose"
            # 安装 pip
            yum install -y python3-pip
            # 使用 pip 安装 docker-compose
            pip3 install docker-compose
            # 建立软链接
            ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose
            docker-compose version # 查看docker-compose版本
            logger success "安装 Docker Compose 完成"
        else
            # 下载 Docker Compose 二进制安装包（非 Amazon Linux 系统）
            curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/sbin/docker-compose
            # 设置可执行权限
            chmod +x /usr/local/sbin/docker-compose
            docker-compose version # 查看docker-compose版本
            logger success "安装 Docker Compose"
        fi
    fi
fi

# 创建数据目录
mkdir -p /data/docker/zabbix/{alertscripts,externalscripts,data}
logger info "创建数据目录完成"

# 检查是否有离线镜像
if [ -f ${CURRENT_DIR}/zabbix_image.tar.gz ]; then
    logger info "检查到离线镜像 [zabbix_image.tar.gz]"
    cat zabbix_image.tar.gz | gzip -d | docker load
    logger success "完成镜像恢复"
fi

logger info "开始服务部署 ... [docker-compose.yaml]"
cd ${CURRENT_DIR}/zabbix-server/docker-compose
docker-compose up -d
logger success "服务部署完成"

# 等待服务启动
sleep 5
# 检查服务状态
logger info "检查服务状态："
docker ps | grep zabbix
logger success "Zabbix 部署完成，请访问 http://服务器IP 进行访问"
logger info "默认用户名: Admin"
logger info "默认密码: zabbix"
